/*
 * ApplicationContext.java
 *
 * Copyright (c) 2007 Teamnet. All Rights Reserved.
 *
 * This source file may not be copied, modified or redistributed,
 * in whole or in part, in any form or for any reason, without the express
 * written consent of Teamnet.
 */
package ro.prjmanager.core.context;

import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import ro.prjmanager.core.cst.Globals;

/**
 * <p> This class stores the action context. It is storing attributes for request, application and
 * session. Also it store the request parameters. Using this class you can put attributes on
 * request, session and application. </p>
 * <p/>
 * manually saved later on the request.
 *
 * @author Teamnet
 */
@SuppressWarnings("unchecked")
public class ApplicationContext {







    private Map sessionMap;
    private Map applicationMap;
    private String serverPath;
    private String serverName;


    private ApplicationContext() {
    }





    /**
     * Return the <u>attributes</u> of the user session. The attributes are stored into a map under
     * the convention <key>,<value> wher key is the name of the attribute and value is the
     * <code>Object</code> value of the attribute.
     *
     * @return a map with session attributes
     */
    public Map getSessionMap() {
        HttpServletRequest request=ApplicationContextFactory.get().getRequest();
        if (sessionMap == null) {
            sessionMap = new SessionMap(request);
        }
        return sessionMap;
    }

    /**
     * Return the <u>attributes</u> of the application. The attributes are stored into a map under
     * the convention <key>,<value> wher key is the name of the attribute and value is the
     * <code>Object</code> value of the attribute.
     *
     * @return the application scope attributes
     */
    public Map getApplicationMap() {
        HttpServletRequest request=ApplicationContextFactory.get().getRequest();
        if (applicationMap == null) {
            applicationMap = new ApplicationMap(request);
        }
        return applicationMap;
    }

    /**
     * <p/>
     * return the serve path as follows: scheme://hostname:port/contextpath/ </p>
     *
     * @return the server path of the application
     */
    public String getServerPath() {

        if (this.serverPath == null) {
            this.serverPath = getRequest().getScheme() + "://"
                    + (serverName == null ? getRequest().getServerName() : serverName) + ":"
                    + getRequest().getServerPort()
                    + getRequest().getContextPath() + "/";
        }
        return serverPath;
    }


    public String getServerName() {
        return serverName;
    }

    /**
     * Set the server name to avoid request.getServerName to return localhost
     *
     * @param serverName the server name
     */
    public void setServerName(String serverName) {
        this.serverName = serverName;
    }

    /**
     * Return the servlet context for this application work session
     *
     * @return the current request
     */
    public static HttpServletRequest getRequest() {
        WebContext webContext= ApplicationContextFactory.get();

        return webContext.getRequest();
    }


    public  static ServletContext getServletContext() {
        HttpSession session=getRequest().getSession();
        if(session!=null)
            return session.getServletContext();
        return null;
    }

    public static HttpSession getSession(){
        return getRequest().getSession();
    }


    public static ApplicationContext getCtx() {
       return new ApplicationContext();
    }


    /**
     * Returns the current user locale or the Locale.getDefault if the user has no locale set.
     *
     * @return lang code as string
     */
    public String getLocaleCode() {
        Locale cLocale = (Locale) getSessionMap().get(Globals.LOCALE_KEY);
        if (cLocale == null) {
            cLocale = Locale.getDefault();
        }
        return cLocale.getLanguage();
    }


    /**
     * Returns the current user locale or the Locale.getDefault if the user has no locale set.
     *
     * @return currentLocale
     */
    public Locale getCurrentLocale() {
        Locale locale = (Locale) getSessionMap().get(Globals.LOCALE_KEY);
        if (locale == null) {
            return Locale.getDefault();
        }
        return locale;
    }

    public void setCurrentLocale(Locale locale) {
        getSessionMap().put(Globals.LOCALE_KEY, locale);
    }



}
